#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
typedef long long LL;
const int MAXN=84;
LL A,B,K;
LL ANS[12][MAXN][MAXN];
LL solve(LL ask) {
	memset(ANS, 0, sizeof(ANS));
	ANS[0][0][0]=1;
	LL tmp=ask;
	for (int i=0; tmp; ++i) {
		for (int j=0; j<K; ++j) {
			for (int k=0; k<K; ++k) {
				LL h=ANS[i][j][k];
				for (int l=0; l<10; ++l) {
					ANS[i+1][(j+l)%K][(k+l*(LL)pow(10, i))%K]+=h;
				}
			}
		}
		tmp=tmp/10;
	}
	LL res=0, cnt=1;
	LL c=ask;
	LL sum=0;
	while (c) {
		sum+=c%10;
		c=c/10;
	}
	c=ask;
	res+=ANS[0][sum%K][c%K];
	while (ask) {
		LL len=ask%10;
		sum-=len;
		c/=10;
		for (int i=0; i<len; ++i) {
			res+=ANS[cnt-1][(K-(sum+i)%K)%K][(K-(c*10+i)*(LL)pow(10, cnt-1)%K)%K];
		}
		ask=ask/10;
		++cnt;
	}
	return res;
}
int main() {
	#ifdef hai
	freopen("in.txt", "r", stdin);
	#endif
	int T;
	scanf("%d", &T);
	while (T--) {
		scanf("%lld%lld%lld", &A, &B, &K);
		if (K>=84) printf("0\n");
		else {
			printf("%lld\n", solve(B)-solve(A-1));
		}
	}
	return 0;
} 
